package net.yla.smartbus.core.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author : 默苍离
 * @Create_Date : 2020/11/14/11:13
 * @Pack_name : net.yla.smartbus.module.oc.cut.utils : FirstDayAndLastDayInMonth
 * *****************************************************
 * 注释:计算任意日期所在周、月、年的第一天与最后一天
 */
public class FirstDayAndLastDayInMonth {


    /**
     * 获取当前月第一天：
     *
     * @return
     */
    public static String getCurrentMonthFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }


    /**
     * 获取当前月第一天(date)：
     *
     * @return
     * @throws ParseException
     */
    public static Date getOneDate() throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        Date parse = format.parse(format.format(cale.getTime()));
        return parse;
    }


    /**
     * 获取当前月最后一天
     *
     * @return
     */
    public static String getCurrentMonthLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale;
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return format.format(cale.getTime());
    }


    /**
     * 根据日期计算当月的第一天与最后一天（返回string类型）
     *
     * @param date
     * @return
     */
    public static Map<String, Object> convertMonthByDate(Date date) {
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //减你妹的1
        //calendar.add(Calendar.MONTH, -1);
        Date theDate = calendar.getTime();
        // 上个月第一天
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        // 上个月最后一天
        calendar.add(Calendar.MONTH, 1); // 加一个月
        calendar.set(Calendar.DATE, 1); // 设置为该月第一天
        calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天
        String day_last = df.format(calendar.getTime());
        map.put("first", day_first);
        map.put("last", day_last);
        return map;
    }


    /**
     * 根据日期计算当月的第一天与最后一天(返回date类型)
     *
     * @param date
     * @return
     */
    public static Map<String, Date> returnDate(Date date) throws ParseException {
        Map<String, Date> map = new HashMap<String, Date>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Date theDate = calendar.getTime();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);

        // 当前月月第一天
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());

        // 加一个月
        calendar.add(Calendar.MONTH, 1);

        // 设置为该月第一天
        calendar.set(Calendar.DATE, 1);

        // 再减一天即为上个月最后一天
        calendar.add(Calendar.DATE, -1);

        calendar.add(Calendar.HOUR_OF_DAY, 23);
        calendar.add(Calendar.MINUTE, 59);
        calendar.add(Calendar.SECOND, 59);

        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = calendar.getTime();
        String day_last = df1.format(time);

        Date f = df.parse(day_first);
        Date l = df1.parse(day_last);
        map.put("first", f);
        map.put("last", l);

        return map;
    }

}
